package io.jee.caisan.weixin.controller.admin;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import io.jee.alaska.util.Result;
import io.jee.caisan.weixin.domain.MpMenu;
import io.jee.caisan.weixin.service.MpMenuService;
import me.chanjar.weixin.common.bean.menu.WxMenuButton;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;

@Controller
@RequestMapping("/admin/mp/menu")
public class MpMenuController {
	
	@Autowired
    private MpMenuService mpMenuService;
	
	@Resource
	private WxMpService wxService;

	@GetMapping
	public String index(Model model) throws WxErrorException {
		model.addAttribute("remoteButtons", mpMenuService.findBySource(MpMenu.SOURCE_REMOTE));
		model.addAttribute("localButtons", mpMenuService.findBySource(MpMenu.SOURCE_LOCAL));
		return "admin/mp/menu";
	}
	
	/**
	 * 同步微信菜单
	 * @return
	 */
	@PostMapping("/async")
	@ResponseBody
	public Result<?> asyncMenu() {
		return mpMenuService.asyncMenu();
	}
	
	@PostMapping("/save")
	@ResponseBody
	public Result<?> saveMenu(@RequestBody List<WxMenuButton> buttons) {
		return mpMenuService.saveMenu(buttons);
	}
	
	@PostMapping("/publish")
	@ResponseBody
	public Result<?> publishMenu() {
		return mpMenuService.publishMenu();
	}
	
	@PostMapping("/delete")
	@ResponseBody
	public Result<?> deleteMenu() {
		return mpMenuService.deleteMenu();
	}
	
}
